﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using CFR.Silverlight.Scheduling;

namespace CFR.Silverlight.Scheduler.Dialogs
{
    public partial class EditEvent : ChildWindow, INotifyPropertyChanged
    {
        private Event _Event;
        public Event Event
        {
            get { return _Event; }
            set { _Event = value; }
        }

        public List<CFR.Silverlight.Scheduling.Calendar> Calendars { get; set; }

        public EditEvent(Event ev, List<CFR.Silverlight.Scheduling.Calendar> calendars)
        {
            Event = ev;
            Calendars = calendars;

            InitializeComponent();

            LoadCalendars();
        }

       
        public bool IsValid()
        {
            if (string.IsNullOrEmpty(txtTitle.Text))
            {
                MessageBox.Show("The \"What\" line is required");
                return false;
            }

            if (tpEndTime.Value.Value.Date != tpStartTime.Value.Value.Date)
            {
                MessageBox.Show("The Start Date and End Date must be in the same day");
                return false;
            }

            if (tpEndTime.Value.Value < tpStartTime.Value.Value)
            {
                MessageBox.Show("The Start Date must come before the End Date");
                return false;
            }

            return true;
        }
      

        private void LoadCalendars()
        {
            cbCalendars.ItemsSource = Calendars;
            cbCalendars.SelectedItem = Calendars.FirstOrDefault(c => c.Name == Event.Calendar.Name);
        }

        private void chkIsAllDayEvent_Checked(object sender, RoutedEventArgs e)
        {
            tpStartTime.Value = tpStartTime.Value.Value.Date;
            tpEndTime.Value = tpEndTime.Value.Value.Date;

            dpEndDate.IsEnabled = tpStartTime.IsEnabled = tpEndTime.IsEnabled = false;
        }

        private void chkIsAllDayEvent_Unchecked(object sender, RoutedEventArgs e)
        {
            dpStartDate.IsEnabled = dpEndDate.IsEnabled = tpStartTime.IsEnabled = tpEndTime.IsEnabled = true;

        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        #endregion

        private void cbCalendars_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Event.Calendar = (cbCalendars.SelectedItem as CFR.Silverlight.Scheduling.Calendar);
        }

        private void btnOkay_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

       
    }
}
